const db = require('../config/db.js');
const generateId = require('../utils/generateId.js');


const orderModel = {
    AddOrder:async({user_id,car_id,order_no = '',rent_time,return_time,duration,cost,state,create_time,remark})=>{

        order_no = generateId();
        const sql = "INSERT INTO `order` (user_id,car_id,order_no,rent_time,return_time,duration,cost,state,create_time,remark) VALUES (?,?,?,?,?,?,?,?,?,?);";
        try {
            const result = await db.query(sql, [user_id,car_id,order_no,rent_time,return_time,duration,cost,state,create_time,remark]);
            console.log(result,'生成订单的结果');
            if(result.length > 0){
                return 1
            }else{
                return  2
            }
        } catch (error) {
            console.log(error, 'OrderModel生成订单失败啦');
        }
    },
    UpdateOrder: async ({id,state,remark=''}) => {
        const sql = 'UPDATE `order` SET remark = ?, state = ? WHERE id = ?;';
        try {
            if(id){
                const res = await db.query(sql,[remark,state,id]);
                console.log(res[0],'更新订单调试');
                return res[0];
            }else{
                return;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    DeleteOrder:async({id})=>{
        console.log(id,'*****');
        const sql ='DELETE FROM `order` WHERE id=?;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'删除订单调试');
                return res[0];
            }else{
                return 0;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    // 获取个人订单
    PersonalOrder: async ({user_id,pageCurrent,OrderKeyword})=> {
        // console.log(pageCurrent,'当前页');
        let current = (pageCurrent - 1) * 10 || 0;
        let size = 10;
        let sql1  = 'SELECT COUNT(DISTINCT `order`.car_id) AS total FROM `order` JOIN car ON `order`.car_id = car.id  WHERE  `order`.user_id = ?';
        let sql2 = `SELECT COUNT(DISTINCT \`order\`.car_id) AS total FROM \`order\` JOIN car ON \`order\`.car_id = car.id WHERE \`order\`.user_id = ? AND  CONCAT(car.brand,car.model) LIKE CONCAT('%', ?, '%');`
        let sql3 = `SELECT \`order\`.id AS id, \`order\`.user_id AS user_id,\`order\`.order_no AS order_no,\`order\`.car_id AS car_id,\`order\`.state AS state,car.brand AS brand,car.model AS model,\`order\`.create_time AS create_time,info,image FROM \`order\` JOIN car ON \`order\`.car_id = car.id  WHERE \`order\`.user_id = ?  GROUP BY \`order\`.id, \`order\`.user_id, \`order\`.car_id, \`order\`.state, car.brand, car.model, \`order\`.create_time, info, image LIMIT ${current},${size};`;
        let sql4 = `SELECT \`order\`.id AS id, \`order\`.user_id AS user_id,\`order\`.order_no AS order_no,\`order\`.car_id AS car_id,\`order\`.state AS state,car.brand AS brand,car.model AS model,\`order\`.create_time AS create_time, info,image FROM \`order\` JOIN car ON \`order\`.car_id = car.id WHERE \`order\`.user_id = ? AND  CONCAT(car.brand,car.model) LIKE CONCAT('%', ?, '%') GROUP BY \`order\`.id, \`order\`.user_id, \`order\`.car_id, \`order\`.state, car.brand, car.model, \`order\`.create_time, info, image LIMIT ${current},${size};`
        try {
            if(OrderKeyword) {
                const res1 = await db.query(sql4, [user_id,OrderKeyword]);
                const res2 = await db.query(sql2,[user_id,OrderKeyword]);
                const [{total}]= res2[0];
                console.log(total, '搜索当前用户订单列表总记录');
                console.log(res1[0], '搜索当前用户订单列表分页带模糊查询');
                return {
                    list: res1[0],
                    total
                }
            }else{
                const res1 = await db.query(sql1,[user_id]);
                const res2 = await db.query(sql3,[user_id]);
                const [{total}]= res1[0];
                console.log(total, '获取当前用户订单列表总记录');
                console.log(res2[0], '获取当前用户订单列表记录分页');
                return {
                    total,
                    list: res2[0]
                }
            }
        }catch (error){
            console.log(error,'OrderModel层 获取用户订单列表出错啦');
        }
    },
    //带分页查询,模糊查询
    GetOrderList: async ({pageCurrent,pageSize,OrderKeyword}) =>{
        console.log(pageCurrent,'当前页');
        console.log(pageSize,'每页显示的条数');
        let current = (pageCurrent - 1) * pageSize || 0;
        let size = pageSize || 10;
        let sql1 = 'SELECT COUNT(*) AS total FROM `order`;';
        let sql2 = `SELECT COUNT(*) AS total FROM \`order\` WHERE CONCAT(order_no,remark) LIKE CONCAT('%', ?, '%')`;
        let sql3 = `SELECT id,order_no,rent_time,return_time,duration,cost,state,create_time,remark FROM \`order\` LIMIT ${current},${size}`;
        let sql4 = `SELECT id,order_no,rent_time,return_time,duration,cost,state,create_time,remark FROM \`order\` WHERE CONCAT(order_no,remark) LIKE CONCAT('%', ?, '%') LIMIT ${current},${size};`
        try {
            if(OrderKeyword) {

                const res1 = await db.query(sql4, [OrderKeyword]);
                const res2 = await db.query(sql2,[OrderKeyword]);
                const [{total}]= res2[0];
                console.log(total, '获取订单列表总记录');
                console.log(res1[0], '获取订单列表记录分页带模糊查询');
                return {
                    list: res1[0],
                    total,
                }
            }else{
                const res1 = await db.query(sql1);
                const res2 = await db.query(sql3);
                const [{total}]= res1[0];
                console.log(total, '获取订单列表总记录');
                console.log(res2[0], '获取订单列表记录分页');
                return {
                    total,
                    list: res2[0]
                }
            }
        } catch (error) {
            console.log(error,'model层获订单列表数据出错啦');
        }
    },
    GetOrderDetail:async ({id}) => {
        console.log(id,'订单id');
        const sql = 'SELECT id,order_no,rent_time,return_time,duration,cost,state,create_time,remark FROM `order` WHERE id = ? ;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'获取订单详情调试');
                return res[0];
            }else{
                return;
            }
        }catch (error) {
            console.log(error,'获取订单详情出错了——Model层');
        }
    }
};

module.exports = orderModel;